package com.cjj.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cjj.common.convention.result.Result;
import com.cjj.common.convention.result.Results;
import com.cjj.common.enums.OrderType;
import com.cjj.dao.entity.SecondhandOrder;
import com.cjj.dto.req.CreateSecondhandProductReqDTO;
import com.cjj.dto.req.CreateServiceOrderDTO;
import com.cjj.dto.req.SearchProductReqDTO;
import com.cjj.dto.req.UpdateBySecondHandIdReqDTO;
import com.cjj.service.SecondhandOrderService;
import com.cjj.service.ServiceOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 二手商品订单表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-10-25
 */
@RestController
@RequestMapping("/api/secondhandOrder")
@RequiredArgsConstructor
public class SecondhandOrderController {

    private final SecondhandOrderService secondhandOrderService;
    private final ServiceOrderService serviceOrderService;

    /**
     * 二手商品发布
     */
    @PostMapping("/create")
    public Result<Void> createRunnerOrder(@RequestBody CreateSecondhandProductReqDTO createSecondhandProductReqDTO) {
        //先创建订单
        CreateServiceOrderDTO serviceOrderDTO = BeanUtil.copyProperties(createSecondhandProductReqDTO, CreateServiceOrderDTO.class);
        serviceOrderDTO.setType(OrderType.SECOND_HAND.getValue());
        serviceOrderService.createOrder(serviceOrderDTO);
        createSecondhandProductReqDTO.setOrderId(serviceOrderDTO.getOrderId());

        //创建二手订单
        secondhandOrderService.createOrder(createSecondhandProductReqDTO);
        return Results.success();
    }

    /**
     * 根据二手订单号修改订单金额
     */
    @PutMapping("/update")
    public Result<Void> updateByOrderId(@RequestBody UpdateBySecondHandIdReqDTO updateBySecondHandIdReqDTO) {
        secondhandOrderService.updateByOrderId(updateBySecondHandIdReqDTO);
        return Results.success();
    }

    /**
     * 模糊查询二手商品
     */
    @GetMapping("/search")
    public Result<IPage<SecondhandOrder>> searchProduct(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody SearchProductReqDTO searchProductReqDTO){
        IPage<SecondhandOrder> page = secondhandOrderService.searchProduct(pageNum,pageSize,searchProductReqDTO);
        return Results.success(page);
    }

}
